package com.easy.web.controller;

import com.easy.web.model.Teacher;
import com.easy.web.service.TeacherService;
import com.easy.web.utils.ViewUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by wang0 on 2017/8/24.
 */
@Controller
@RequestMapping("teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;


    /**
     * 获取老师列表
     */
    @RequestMapping(value = "getTeacherList", method = {RequestMethod.GET})
    public ModelAndView getTeacherList() {
        List<Teacher> teacherList = teacherService.getTeacherList();

        return new ModelAndView("", ViewUtils.mapSuccess(teacherList));
    }


    /**
     * 新增一个老师
     */
    @RequestMapping(value = "addTeacher", method = {RequestMethod.POST})
    public ModelAndView addTeacher(@RequestParam(value = "setTeacherName", required = true) String name,
                                   @RequestParam(value = "setTeacherGender", required = true) String gender,
                                   @RequestParam(value = "setTeacherAge", required = true) String age,
                                   @RequestParam(value = "setTeacherTitle", required = true) String title,
                                   @RequestParam(value = "setTeacherComment", required = true) String comment,
                                   @RequestParam(value = "setTeacherImg", required = true) String img) {
        Teacher teacher = new Teacher();
        teacher.setName(name);
        teacher.setGender(gender);
        teacher.setAge(age);
        teacher.setTitle(title);
        teacher.setComment(comment);
        teacher.setCreateTime(new Date());
        teacher.setId(UUID.randomUUID().toString());
        teacher.setImg(img);
        try {
            teacherService.addTeacher(teacher);
        } catch (Exception e) {
            e.printStackTrace();
            return new ModelAndView("", ViewUtils.mapFail("新增失败"));
        }

        return new ModelAndView("", ViewUtils.mapSuccess("success"));
    }

    /**
     * 删除一个老师
     */
    @RequestMapping(value = "delTeacher", method = {RequestMethod.POST})
    public ModelAndView delTeacher(@RequestParam(value = "delTeacherUid", required = true) String id) {

        teacherService.delTeacher(id);
        return new ModelAndView("", ViewUtils.mapSuccess("success"));
    }


    /**
     * 更新一个老师信息
     */
    @RequestMapping(value = "updateTeacher", method = {RequestMethod.POST})
    public ModelAndView updateTeacher(@RequestParam(value = "editTeacherUid", required = true) String id,
                                      @RequestParam(value = "editTeacherName", required = true) String name,
                                      @RequestParam(value = "editTeacherGender", required = true) String gender,
                                      @RequestParam(value = "editTeacherAge", required = true) String age,
                                      @RequestParam(value = "editTeacherTitle", required = true) String title,
                                      @RequestParam(value = "editTeacherComment", required = true) String comment,
                                      @RequestParam(value = "editTeacherImg", required = true) String img) {

        Teacher teacher = teacherService.queryTeacherById(id);
        if (teacher == null) {
            return new ModelAndView("", ViewUtils.mapFail("编辑的该老师不存在，请重新编辑"));
        } else {
            teacher.setName(name);
            teacher.setGender(gender);
            teacher.setAge(age);
            teacher.setTitle(title);
            teacher.setComment(comment);
            teacher.setImg(img);
            try {
                teacherService.updateTeacher(teacher);
            } catch (Exception e) {
                e.printStackTrace();
                return new ModelAndView("", ViewUtils.mapFail("更新失败"));
            }


            return new ModelAndView("", ViewUtils.mapSuccess("success"));
        }
    }
}
